#include "jvkbutton.h"
#include <QMouseEvent>
#include <QTimerEvent>

namespace QTE
{

class Q_DECL_HIDDEN JVKButtonPrivate : public QObject
{
public:
	using QObject::QObject;

public:
	void start();
	void stop();

protected:
	void timerEvent(QTimerEvent *event) override;

private:
	int m_timerID = -1;
};

inline void JVKButtonPrivate::start()
{
	if( m_timerID < 0 )
		m_timerID = startTimer(600);
}

inline void JVKButtonPrivate::stop()
{
	if( m_timerID > 0 )
	{
		killTimer(m_timerID);
		m_timerID = -1;
	}
}

void JVKButtonPrivate::timerEvent(QTimerEvent *event)
{
	QObject::timerEvent(event);
	if( event->timerId() == m_timerID )
		emit reinterpret_cast<JVKButton*>(parent())->actionTrigger(JVKButton::LongPress);
}

/*-------------------------------------------------------------------------------------------------------*/

JVKButton::JVKButton(QWidget *parent) :
	QToolButton(parent),
	d_ptr(new JVKButtonPrivate(this))
{

}

JVKButton::~JVKButton()
{

}

void JVKButton::mousePressEvent(QMouseEvent *event)
{
	QToolButton::mousePressEvent(event);

	if( event->button() == Qt::LeftButton )
		d_ptr->start();

	else if( event->button() == Qt::RightButton )
		emit actionTrigger(RightPress);
}

void JVKButton::mouseReleaseEvent(QMouseEvent *event)
{
	QToolButton::mouseReleaseEvent(event);

	if( event->button() == Qt::LeftButton )
		d_ptr->stop();
}

} //namespace QTE
